package com.liuzhu.chaoshikucungunli.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.liuzhu.chaoshikucungunli.R;
import com.liuzhu.chaoshikucungunli.LoginActivity;
import com.liuzhu.chaoshikucungunli.db.UserDao;
import com.liuzhu.chaoshikucungunli.model.User;
import com.liuzhu.chaoshikucungunli.utils.SharedPreferencesManager;

public class ProfileFragment extends Fragment {
    private ImageView avatarImage;
    private TextView nameText;
    private TextView usernameText;
    private TextView roleText;
    private TextView companyText;
    private TextView phoneText;
    private SharedPreferencesManager prefsManager;
    private UserDao userDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        
        // 初始化
        initViews(view);
        prefsManager = SharedPreferencesManager.getInstance(requireContext());
        userDao = new UserDao(requireContext());
        
        // 加载用户信息
        loadUserInfo();
        
        // 设置点击事件
        setupClickListeners(view);
        
        return view;
    }

    private void initViews(View view) {
        avatarImage = view.findViewById(R.id.profile_avatar);
        nameText = view.findViewById(R.id.profile_name);
        usernameText = view.findViewById(R.id.text_username);
        roleText = view.findViewById(R.id.text_role);
        companyText = view.findViewById(R.id.text_company);
        phoneText = view.findViewById(R.id.text_phone);
    }

    private void loadUserInfo() {
        // 从数据库获取完整的用户信息
        User user = userDao.getUserByUsername(prefsManager.getUsername());
        if (user != null) {
            nameText.setText(user.getUsername());
            usernameText.setText(user.getUsername());
            roleText.setText(user.getRole());
            companyText.setText(user.getCompany());
            phoneText.setText(user.getPhone());
            
            // TODO: 加载用户头像
            if (user.getAvatar() != null && !user.getAvatar().isEmpty()) {
                // 设置头像
            }
        }
    }

    private void setupClickListeners(View view) {
        // 退出登录按钮点击事件
        view.findViewById(R.id.btn_logout).setOnClickListener(v -> logout());

        // 其他菜单项点击事件
        view.findViewById(R.id.layout_username).setOnClickListener(v -> {
            // TODO: 修改用户名
        });

        view.findViewById(R.id.layout_role).setOnClickListener(v -> {
            // TODO: 查看角色信息
        });

        view.findViewById(R.id.layout_company).setOnClickListener(v -> {
            // TODO: 修改公司信息
        });

        view.findViewById(R.id.layout_phone).setOnClickListener(v -> {
            // TODO: 修改手机号
        });

        view.findViewById(R.id.layout_change_password).setOnClickListener(v -> {
            // TODO: 修改密码
        });
    }

    private void logout() {
        // 显示确认对话框
        new AlertDialog.Builder(requireContext())
            .setTitle("退出登录")
            .setMessage("确定要退出登录吗？")
            .setPositiveButton("确定", (dialog, which) -> {
                // 清除登录信息
                prefsManager.logout();
                
                // 显示退出成功提示
                Toast.makeText(requireContext(), "退出登录成功", Toast.LENGTH_SHORT).show();
                
                // 跳转到登录界面
                Intent intent = new Intent(requireContext(), LoginActivity.class);
                // 清除任务栈中的其他Activity
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            })
            .setNegativeButton("取消", null)
            .setIcon(R.drawable.ic_warning)
            .show();
    }
} 